package village.rainbow.dailyscene.adapter;

import village.rainbow.dailyscene.R;
import village.rainbow.dailyscene.datamodel.DataRow;
import village.rainbow.dailyscene.datamodel.DataTable;
import village.rainbow.dailyscene.uicontrol.SceneView;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

public class ViewPagerAdapter extends PagerAdapter{

	private LayoutInflater mInflater;
	private DataTable mItems = new DataTable();
	private OnClickListener mListener;
	private Context mContext;
	private boolean mEditTextDisabled = true; 
	
	public ViewPagerAdapter(Context c, DataTable items){
		this(c, items, true);
	}
	
	public ViewPagerAdapter(Context c, DataTable items, boolean editTextEnable){
		super();
		mContext = c;
		mInflater = LayoutInflater.from(c);
		mItems = items;
		mEditTextDisabled = editTextEnable;
	}
	
	@Override
	public Object instantiateItem(View pager, int position) {
		
		View view = mInflater.inflate(R.layout.pager_item, null);
		SceneView v = (SceneView)view;
		v.init(mContext);
		ImageView imv = (ImageView)v.findViewById(R.id.imv_picture);
		EditText et = (EditText)v.findViewById(R.id.edt_memo);
		
		DataRow row = mItems.get(position);
		
		String picturePath = row.getString("SCENE_PIC_LOCATION", "");
		String sceneText = row.getString("SCENE_TEXT", "");
		et.setText(sceneText);
		et.setEnabled(mEditTextDisabled);
		if(!mEditTextDisabled){
			et.setBackgroundColor(Color.argb(150, 110, 134, 150));
		}
		
		Drawable d = BitmapDrawable.createFromPath(picturePath);
		if(d != null){
			imv.setImageDrawable(d);
		}
		
		v.setTag(row);
		((ViewPager)pager).addView(v, 0);
		
		imv.setClickable(true);
		imv.setOnClickListener(mListener);
		return v; 
	}
	
	@Override
	public void destroyItem(View pager, int position, Object view) {	
		((ViewPager)pager).removeView((View)view);
	}
	
	@Override
	public boolean isViewFromObject(View pager, Object obj) {
		return pager == obj; 
	}
	
	public void add(DataRow row){
		mItems.add(row);
		this.notifyDataSetChanged();
	}
	
	public void add(int position, DataRow row){
		mItems.add(position, row);
		this.notifyDataSetChanged();
	}
	
	public void addAll(DataTable list){
		mItems = list;
		this.notifyDataSetChanged();
	}
	
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		Log.i("TEST", "instantiateItem \n" + position);
		return super.instantiateItem(container, position);
	}
	
	@Override 
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		Log.i("TEST", "restoreState \n" + arg0.toString() + " / " + arg1.toString());
	}
	
	@Override 
	public Parcelable saveState() { 
		Log.i("TEST", "saveState \n");
		return null; 
	}
	
	@Override 
	public void startUpdate(View arg0) {
		ViewPager pager = (ViewPager)arg0;
		Log.i("TEST", "startUpdate \n" + pager.getCurrentItem());
	}
	
	@Override 
	public void finishUpdate(View arg0) {
		ViewPager pager = (ViewPager)arg0;
		Log.i("TEST", "finishUpdate \n" + pager.getCurrentItem());
	}
	
	@Override
	public float getPageWidth(int position) {
		return super.getPageWidth(position);///9*8;
	}
	
	@Override
	public int getCount() {
		return mItems.size();
	}
	
	public void setOnClickListener(OnClickListener listener){
		mListener = listener;
	}
	
	public void clear() {
		mItems = new DataTable();
	}
	
	public DataTable getItems(){
		return mItems;
	}
	
}